﻿namespace SPWikiProvisioning.Extensions
{
    #region Usings

    using System;
    using System.Globalization;
    using System.IO;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;

    #endregion

    internal static class SPFeatureExtensions
    {
        internal static SPWeb GetRelatedSPWeb(this SPFeature feature)
        {
            var web = feature.Parent as SPWeb;
            if (web == null)
            {
                throw new SPException(SPResource.GetString("NoWebException", new object[0]));
            }

            return web;
        }

        public static string GetFeatureDefinitionFilePath(this SPFeature feature, string fileName)
        {
            var sourcePath = String.Format(CultureInfo.CurrentCulture,
                @"{0}\FEATURES\{1}\{2}",
                SPUtility.GetGenericSetupPath("Template"),
                feature.Definition.DisplayName,
                fileName);

            return sourcePath;
        }

        public static string GetFeatureDefinitionFileContents(this SPFeature feature, string fileName)
        {
            var sourcePath = feature.GetFeatureDefinitionFilePath(fileName);
            var stream = File.OpenText(sourcePath);
            var data = stream.ReadToEnd();
            return data;
        }

    }
}
